'use strict';

const path = require('path');
const os = require('os');

const { Sequelize } = require('sequelize');
const Op = Sequelize.Op;

const mysqldatabase = 'CLOVER';
const mysqlhost = '127.0.0.1';
const mysqlpassward = '123456';
const redishost = '127.0.0.1';
const redispassword = '123456';

module.exports = appInfo => {
  const config = exports = {};

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1543238215036_7785';

  // add your config here
  config.middleware = ['errorHandler'];

  config.alinode = {
    server: 'wss://agentserver.node.aliyun.com:8080',
    appid: '77811',
    secret: 'b0797d98785d6bec54be67f037a9978ea096a5c1',
  };

  config.oss = {
    client: {
      accessKeyId: 'LTAIWc3gzUKzuP0g',
      accessKeySecret: 'bnegIC2gziCbifcLUMKJE62qoh2KZo',
      bucket: 'clover-mini-program',
      endpoint: 'oss-cn-beijing.aliyuncs.com',
      timeout: '60s',
    },
  };

  config.weappSDK = {
    appId: 'wx745b3ac635523397',
    appSecret: '1427dffae8b23d9ff95acda8e7baf753',
  };

  config.redis = {
    clients: {
      expired: {
        port: 6379,
        host: redishost,
        password: redispassword,
        db: 0,
      },
      expired_subscribe: {
        port: 6379,
        host: redishost,
        password: redispassword,
        db: 0,
      },
      goods: {
        port: 6379,
        host: redishost,
        password: redispassword,
        db: 1,
      },
      users: {
        port: 6379,
        host: redishost,
        password: redispassword,
        db: 2,
      }
    },
  };

  config.sequelize = {
    dialect: 'mysql',
    username: 'root',
    port: 3306,
    host: mysqlhost,
    database: mysqldatabase,
    password: mysqlpassward,
    define: {
      paranoid: true,
      underscored: false,
      timestamps: true,
      charset: 'utf8mb4',
      dialectOptions: {
        collate: 'utf8mb4_general_ci',
      },
    },
    timezone: '+08:00',
    operatorsAliases: {
      $eq: Op.eq,
      $ne: Op.ne,
      $gte: Op.gte,
      $gt: Op.gt,
      $lte: Op.lte,
      $lt: Op.lt,
      $not: Op.not,
      $in: Op.in,
      $notIn: Op.notIn,
      $is: Op.is,
      $like: Op.like,
      $notLike: Op.notLike,
      $iLike: Op.iLike,
      $notILike: Op.notILike,
      $regexp: Op.regexp,
      $notRegexp: Op.notRegexp,
      $iRegexp: Op.iRegexp,
      $notIRegexp: Op.notIRegexp,
      $between: Op.between,
      $notBetween: Op.notBetween,
      $overlap: Op.overlap,
      $contains: Op.contains,
      $contained: Op.contained,
      $adjacent: Op.adjacent,
      $strictLeft: Op.strictLeft,
      $strictRight: Op.strictRight,
      $noExtendRight: Op.noExtendRight,
      $noExtendLeft: Op.noExtendLeft,
      $and: Op.and,
      $or: Op.or,
      $any: Op.any,
      $all: Op.all,
      $values: Op.values,
      $col: Op.col
    }
  };

  config.bcrypt = {
    saltRounds: 10 // default 10
  };

  // 关闭安全威胁csrf的防范
  config.security = {
    csrf: {
      enable: false,
      // ignoreJSON: true, // 默认为 false，当设置为 true 时，将会放过所有 content-type 为 `application/json` 的请求
    },
    // domainWhiteList: ['http://localhost:8000'],
  };

  config.jwt = {
    secret: '123456',
    // enable: true, // default is false
    // match: '/api', // optional
  };

  config.errorHandler = {
    match: '/api',
  };

  // 部署环境
  config.domain = 'http://127.0.0.1:7001';

  // -------------------------微信相关---------------------------
  config.wechat = {
    AppID: 'wx745b3ac635523397',
    AppSecret: '1427dffae8b23d9ff95acda8e7baf753'
  };
  // -----------------------------------------------------------

  // ------------------------七牛云相关---------------------------
  config.qiniu = {
    // I ussually set the key into `~/.zshrc`, and I can get the value via `process.env.key`, It's very safe~
    ak: '-6x5xZO4so9aL1dGAkoe2pzWdIkH37jW_nDLjO0w',
    sk: '-6x5xZO4so9aL1dGAkoe2pzWdIkH37jW_nDLjO0w',
    bucket: 'clover',
    baseUrl: 'piy5kv2h2.bkt.clouddn.com',
    // zone: '',
    app: true,
    agent: false,
  };

  config.multipart = {
    mode: 'file',
    // fileExtensions: ['.apk', '.pptx', '.docx', '.csv', '.doc', '.ppt', '.pdf', '.pages', '.wav', '.mov'], // 增加对 .apk 扩展名的支持
    // tmpdir: path.join(os.tmpdir(), 'egg-multipart-tmp', appInfo.name),
    // cleanSchedule: {
    // run tmpdir clean job on every day 04:30 am
    // cron style see https://github.com/eggjs/egg-schedule#cron-style-scheduling
    // cron: '0 30 4 * * *',
    // },
  };

  return config;
};